using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PartsList : MonoBehaviour
{
    public RectTransform m_PartsContent;
    public RectTransform m_PartsTitle;
    public List<GameObject> m_PartsItemList = new List<GameObject>();

    public GameObject m_BatteryItemPrefab;
    public GameObject m_EngineItemPrefab;
    public GameObject m_DynamoItemPrefab;
    public GameObject m_HookItemPrefab;
    public GameObject m_ShallItemPrefab;
    public GameObject m_ShildItemPrefab;
    public GameObject m_WeaponSlotItemPrefab;
    public GameObject m_OtherItemPrefab;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void Init(Item.ItemType itemType)
    {
        for(int i=0; i<m_PartsItemList.Count; ++i)
        {
            Destroy(m_PartsItemList[i]);
        }
        m_PartsItemList.Clear();

        GameObject objPrefab = null;
        List<Item> itemList = null;
        switch(itemType)
        {
            case Item.ItemType.BATTERY:
                itemList = ItemManager.Instance.m_Batteries;
                objPrefab = m_BatteryItemPrefab;
                break;
            case Item.ItemType.ENGINE:
                itemList = ItemManager.Instance.m_Engines;
                objPrefab = m_EngineItemPrefab;
                break;
            case Item.ItemType.DYNAMO:
                itemList = ItemManager.Instance.m_Dynamos;
                objPrefab = m_DynamoItemPrefab;
                break;
            case Item.ItemType.HOOK:
                itemList = ItemManager.Instance.m_Hook;
                objPrefab = m_HookItemPrefab;
                break;
            case Item.ItemType.SHILD:
                itemList = ItemManager.Instance.m_Shild;
                objPrefab = m_ShildItemPrefab;
                break;
            case Item.ItemType.WEAPON_SLOT:
                itemList = ItemManager.Instance.m_WeaponSlots;
                objPrefab = m_WeaponSlotItemPrefab;
                break;
            case Item.ItemType.SHALL:
                itemList = ItemManager.Instance.m_Shall;
                objPrefab = m_ShallItemPrefab;
                break;
            case Item.ItemType.OTHER_PART:
                itemList = ItemManager.Instance.m_OtherParts;
                objPrefab = m_OtherItemPrefab;
                break;
            default:
                return;
        }
        for (int i = 0; i < itemList.Count; ++i)
        {
            var newItemUI = Instantiate(objPrefab);
            newItemUI.GetComponent<RectTransform>().SetParent(m_PartsContent.transform);
            newItemUI.GetComponent<RectTransform>().SetSiblingIndex(0);
            m_PartsItemList.Add(newItemUI);
            var itemItem = newItemUI.GetComponent<ItemItem>();
            itemItem.Init(itemList[i]);


            var size = itemItem.GetComponent<RectTransform>().sizeDelta;
            size.y = 100;
            itemItem.GetComponent<RectTransform>().sizeDelta = size;
        }

    }

    public void OnBatteryClicked()
    {
        Init(Item.ItemType.BATTERY);
    }
    public void OnEnginesClicked()
    {
        Init(Item.ItemType.ENGINE);
    }
    public void OnDynamosClicked()
    {
        Init(Item.ItemType.DYNAMO);
    }
    public void OnHookClicked()
    {
        Init(Item.ItemType.HOOK);
    }
    public void OnShallClicked()
    {
        Init(Item.ItemType.SHALL);
    }
    public void OnShildClicked()
    {
        Init(Item.ItemType.SHILD);
    }
    public void OnWeaponSlotClicked()
    {
        Init(Item.ItemType.WEAPON_SLOT);
    }
    public void OnOtherClicked()
    {
        Init(Item.ItemType.OTHER_PART);
    }

}
